// pages/my/my-integral/index.js
import { fetchUserSocrelog } from '~/request/api';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    socre: 0,
    list: [],
    hasLoaded: false,
    loadMoreStatus: 0,
  },

  // 分页变量
  total: 0,
  page: 1,
  page_size: 10,

  // 获取借阅记录列表
  async socrelogList(isPage = false) {
    const { page, page_size } = this;
    const { list, loadMoreStatus } = this.data;
    const _page = isPage ? page + 1 : page;
    let query = { page: _page, page_size };
    if (loadMoreStatus !== 0) return;
    this.setData({
      loadMoreStatus: 1,
    });
    try {
      const res = await fetchUserSocrelog(query);
      const { data, total, socre } = res;
      if (total === 0) {
        this.total = total;
        this.setData({
          hasLoaded: true,
          loadMoreStatus: 0,
          list: [],
          socre: 0,
        });
        return;
      }
      const _list = list.concat(data);
      const _loadMoreStatus = list.length === total ? 2 : 0;
      this.page = query?.page || 1;
      this.total = total;
      this.setData({
        list: _list,
        loadMoreStatus: _loadMoreStatus,
        socre,
      });
    } catch (error) {
      console.log(error);
    }
    this.setData({
      hasLoaded: true,
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.socrelogList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const { list } = this.data;
    const { total = 0 } = this;
    if (list.length === total) {
      this.setData({
        loadMoreStatus: 2,
      });
      return;
    }
    this.socrelogList(true);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})